# -*- coding: utf-8 -*-
#
# File: BasicMenu.py
#
# Copyright (c) 2007 by Copyright (c) 2007 P2P Informatica LTDA
# Generator: ArchGenXML Version 1.5.2
#            http://plone.org/products/archgenxml
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """Pablo <pablo@p2pinformatica.com>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.PMenuTool.config import *

# additional imports from tagged value 'import'
from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget

##code-section module-header #fill in your manual code here
##/code-section module-header

schema = Schema((

    ReferenceField(
        name='LinkInterno',
        index="FieldIndex",
        widget=ReferenceBrowserWidget(
            startup_directory="/",
            label="Link Interno",
            description="Informe algo neste campo caso queira um item de menu que aponte para um objeto interno do site.",
            label_msgid='PMenuTool_label_LinkInterno',
            description_msgid='PMenuTool_help_LinkInterno',
            i18n_domain='PMenuTool',
        ),
        relationship='Rel1',
        multiValued=0,
        searchable=1,
        allow_browse="1"
    ),

    StringField(
        name='LinkExterno',
        index="FieldIndex",
        widget=StringWidget(
            label="Link Externo",
            description="Informa algo neste campo para um item de menu que aponte para uma pagina externa ao site.",
            label_msgid='PMenuTool_label_LinkExterno',
            description_msgid='PMenuTool_help_LinkExterno',
            i18n_domain='PMenuTool',
        ),
        validators=('isURL',)
    ),

    StringField(
        name='LinkPageTemplate',
        index="FiedIndex",
        widget=StringWidget(
            label="Link para template",
            description="Informe algo neste campo para apontar o item de menu para algum template padrão do plone. Exemplo: Mapa do Site e Faleconosco.",
            label_msgid='PMenuTool_label_LinkPageTemplate',
            description_msgid='PMenuTool_help_LinkPageTemplate',
            i18n_domain='PMenuTool',
        )
    ),

    StringField(
        name='ModoVisualizacao',
        index="FieldIndex",
        required=1,
        widget=SelectionWidget(
            label="Modo de Visualização",
            description="Selecione como será o modo de visualização deste item de menu.",
            label_msgid='PMenuTool_label_ModoVisualizacao',
            description_msgid='PMenuTool_help_ModoVisualizacao',
            i18n_domain='PMenuTool',
        ),
        enforce_vocabulary="1",
        vocabulary='getModosVisualizacao_vocab'

    ),

    StringField(
        name='DestinoLink',
        index="FieldIndex",
        required=1,
        widget=SelectionWidget(
            label="Destino do link",
            description="Selecione o destino do link do item de menu.",
            label_msgid='PMenuTool_label_DestinoLink',
            description_msgid='PMenuTool_help_DestinoLink',
            i18n_domain='PMenuTool',
        ),
        enforce_vocabulary="1",
        vocabulary='getDestinosLink_vocab'

    ),

),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

BasicMenu_schema = BaseSchema.copy() + \
    schema.copy()

##code-section after-schema #fill in your manual code here
##/code-section after-schema

class BasicMenu:
    """
    """
    security = ClassSecurityInfo()

    # This name appears in the 'add' box
    archetype_name = 'BasicMenu'

    meta_type = 'BasicMenu'
    portal_type = 'BasicMenu'
    allowed_content_types = []
    filter_content_types = 0
    global_allow = 1
    #content_icon = 'BasicMenu.gif'
    immediate_view = 'base_view'
    default_view = 'base_view'
    suppl_views = ()
    typeDescription = "BasicMenu"
    typeDescMsgId = 'description_edit_basicmenu'

    _at_rename_after_creation = True

    schema = BasicMenu_schema

    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods

    security.declarePublic('getDestinosLink_vocab')
    def getDestinosLink_vocab(self):
        vocabulary = (
                        ['_self', 'Mesma Janela'],
                        ['_blank', 'Outra Janela'],
                     )
        return DisplayList(vocabulary)

    security.declarePublic('getModosVisualizacao_vocab')
    def getModosVisualizacao_vocab(self):
        vocabulary = (
                        ['adm', 'Administrador'],
                        ['pub', 'Público'],
                     )
        return DisplayList(vocabulary)

    def getLinkLocation(self):
        portal_url = self.portal_url()
        #import pdb; pdb.set_trace()
        interno =  self.getReferenceImpl('Rel1')
        externo = self.getLinkExterno()
        template = self.getLinkPageTemplate()

        if interno:
            obj = interno[0].getTargetObject()
            interno_path = '/'.join(obj.getPhysicalPath())
            return interno_path
        elif externo:
            return externo
        elif template:
            return portal_url + '/' +template
        else:
            return '#'



# end of class BasicMenu

##code-section module-footer #fill in your manual code here
##/code-section module-footer



